/**
 * Created with JetBrains PhpStorm.
 * User: vander
 * Date: 5/27/13
 * Time: 3:38 PM
 * To change this template use File | Settings | File Templates.
 */

var Favorite = Class.create({
    initialize: function(){
        this.books = new Array();
    },
    load: function(o){
        var _this = this;

        $j.each(o.books, function(index, value){
            var bookInfo = new Object();

            $j.each(value, function(key, val){
                bookInfo[key] = val;
            });

            _this.books.push(bookInfo);
        });
        $j("#"+Static.favoriteList).empty();

        $j.each(_this.books, function(index, value){
            var liTag = new Element("li", {bookId: value.bookId});
            var img = $j("<img>").attr('src',Global.localPath +  value.resourcesPath + value.coverImage).attr({width: "80", height: "80"});
            var aTag = $j("<a href='#'></a>").append(img).append(value.name);
            var splitaTag = $j("<a href='#favorite'>Remove</a>");

            $j(splitaTag).on("tap", function(){
                _this.remove(value.bookId);
            });

            $j(aTag).on("tap", function(){
                Global.collections.viewBook(value.bookId);
            });

            $j(liTag).append(aTag).append(splitaTag).appendTo("#" + Static.favoriteList);
        });
        $j("#"+Static.favoriteList).listview("refresh");
    },
    add: function(bookInfo){
        var _this = this;

        var existed = false;
        //Check if already in favorite list
        for(var i = 0 ; i < this.books.length; i++){
            if(this.books[i].bookId == bookInfo.bookId){
                existed = true;
                break;
            }
        }

        if(!existed){
            this.books.push(bookInfo);
            var value = bookInfo;

            var liTag = new Element("li", {bookId: value.bookId});
            var img = $j("<img>").attr('src',Global.localPath +  value.resourcesPath + value.coverImage).attr({width: "80", height: "80"});
            var aTag = $j("<a href='#'></a>").append(img).append(value.name);
            var splitaTag = $j("<a href='#favorite'>Remove</a>");

            $j(splitaTag).on("tap", function(){
                _this.remove(value.bookId);
            });

            $j(aTag).on("tap", function(){
                $j(aTag).on("tap", function(){
                    Global.collections.viewBook(value.bookId);
                });
            });

            $j(liTag).append(aTag).append(splitaTag).appendTo("#" + Static.favoriteList);

            $j("#"+Static.favoriteList).listview("refresh");

            this.save();
        }
    },
    remove: function(bookId){
        for(var i = 0 ; i < this.books.length; i++){
            if(this.books[i].bookId == bookId){
                var liTag = $j("#"+Static.favoriteList + " li[bookId=" + bookId +"]");
                $j(liTag).remove();

                this.books[i] = null;
                this.books = this.books.compact();
                $j("#"+Static.favoriteList).listview("refresh");

                this.save();
                break;
            }
        }
    },
    save: function(){
        MyTools.saveTextPersistent("favorite.json", $j.toJSON({books: this.books}));
    }
});
